﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyQuanCafe.DataAccess;
using QuanLyQuanCafe.BusinessObject;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace QuanLyQuanCafe.BusinessLogic
{
    class CT_NhapKhoBL
    {
        KetNoiDuLieu connect = new KetNoiDuLieu();
        public DataTable DSCT_NhapKho()
        {
            string sql = "SELECT NGUYENLIEU.TenNL, CT_PHIEUNHAP.SoLuong, CT_NHAPKHO.DonGia, CT_NHAPKHO.ThanhTien, DVT.TenDVT,  FROM NGUYENLIEU, CT_NHAPKHO, DVT WHERE NGUYENLIEU.MaNL = CT_NHAPKHO.MaNL AND CT_NHAPKHO.MaDVT = DVT.TenDVT";
            return connect.LayDanhSachDuLieu(sql);
        }

        public DataTable DSNhapKhoNguyenLieu(int value)
        {
            string sql = "SELECT NGUYENLIEU.TenNL, CT_PHIEUNHAP.SoLuong, CT_PHIEUNHAP.DonGia, CT_PHIEUNHAP.ThanhTien, DVT.TenDVT  FROM NGUYENLIEU, CT_PHIEUNHAP, DVT WHERE NGUYENLIEU.MaNL = CT_PHIEUNHAP.MaNL AND CT_PHIEUNHAP.MaDVT = DVT.MaDVT AND CT_PHIEUNHAP.MaNhap = '" + value + "'";
            return connect.LayDanhSachDuLieu(sql);
        }

        public float TongTien(int value) 
        {
            float tongtien =0;
            string sql = "SELECT ThanhTien FROM CT_PHIEUNHAP WHERE MaNhap ='"+value+"'";
            DataTable thanhtiendt= connect.LayDanhSachDuLieu(sql);
            if (thanhtiendt.Rows.Count > 0)
            {
                for (int i = 0; i < thanhtiendt.Rows.Count; i++)
                {
                    tongtien += float.Parse(thanhtiendt.Rows[i]["ThanhTien"].ToString());
                }
            }
            return tongtien;
        }
        public bool CheckCTNhapKho(int value1)
        {
            if (connect.KiemTraGiaTriInt("CT_PHIEUNHAP", "MaNhap", value1))
                return true;
            return false;
        }

        public bool CheckCTNL(int value1)
        {
            if (connect.KiemTraGiaTriInt("CT_PHIEUNHAP", "MaNL", value1))
                return true;
            return false;
        }


        public bool ThemCTNhapKho(CT_NhapKhoBO ctnhapkhoBO)
        {
            if (TestSave(ctnhapkhoBO))
            {
                string sql = string.Format("INSERT INTO CT_PHIEUNHAP VALUES ('{0}', '{1}', '{2}','{3}','{4}','{5}')", ctnhapkhoBO.MaNhap, ctnhapkhoBO.MaNL, ctnhapkhoBO.SoLuong, ctnhapkhoBO.DonGia, ctnhapkhoBO.ThanhTien, ctnhapkhoBO.MaDVT);
                if (connect.ExecuteQuery(sql))
                {
                    MessageBox.Show("Thêm Thành Công", "Quản Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return true;
                }
            }
            return false;
        }

        public bool SuaCTNhapKho(CT_NhapKhoBO ctnhapkhoBO)
        {
            if (TestSave(ctnhapkhoBO))
            {
                string sql = string.Format("UPDATE CT_PHIEUNHAP SET MaNL = '{0}', SoLuong = '{1}', DonGia = '{2}', MaDVT='{3}', ThanhTien ='{4}'" + "WHERE MaNhap = '{5}'", ctnhapkhoBO.MaNL, ctnhapkhoBO.SoLuong, ctnhapkhoBO.DonGia, ctnhapkhoBO.MaDVT, ctnhapkhoBO.ThanhTien, ctnhapkhoBO.MaNhap);
                if (connect.ExecuteQuery(sql))
                {
                    MessageBox.Show("Sửa Thành Công", "Quản Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return true;
                }
            }
            return false;
        }

        public bool SuaCTNKNL(CT_NhapKhoBO ctnhapkhoBO)
        {
            if (TestSave(ctnhapkhoBO))
            {
                string sql = string.Format("UPDATE CT_PHIEUNHAP SET  SoLuong = '{0}', DonGia = '{1}', MaDVT='{2}', ThanhTien ='{3}'" + "WHERE MaNhap = '{4}' AND MaNL ='{5}'", ctnhapkhoBO.SoLuong, ctnhapkhoBO.DonGia, ctnhapkhoBO.MaDVT, ctnhapkhoBO.ThanhTien, ctnhapkhoBO.MaNhap, ctnhapkhoBO.MaNL);
                if (connect.ExecuteQuery(sql))
                {
                    MessageBox.Show("Sửa Thành Công", "Quản Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return true;
                }
            }
            return false;
        }

        public bool XoaCTNhapKho(string value1, string value2)
        {
            string sql = string.Format("DELETE FROM CT_PHIEUNHAP WHERE MaNL = '" + value1 + "' AND MaNhap = '" + value2 + "'");
            if (connect.ExecuteQuery(sql))
            {
                MessageBox.Show("Xóa Thành Công", "Quản Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            return false;
        }

        public bool XoaCTMaNhap(string value)
        {
            string sql = string.Format("DELETE FROM CT_PHIEUNHAP WHERE MaNhap = '" + value + "'");
            if (connect.ExecuteQuery(sql))
            {
                return true;
            }
            return false;
        }

        public bool TestSave(CT_NhapKhoBO ctnhapkhoBO)
        {
            if (ctnhapkhoBO.DonGia<0)
            {
                MessageBox.Show("Đơn Giá Phải Lớn Hơn 0", "Quản Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (ctnhapkhoBO.ThanhTien==0)
            {
                MessageBox.Show("Chưa Thêm Nguyên Liệu Cần", "Quản Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (ctnhapkhoBO.SoLuong < 0)
            {
                MessageBox.Show("Số lượng Nguyên Liệu Phải Lớn Hơn 0", "Quản Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            return true;
        }
    }
}
